/*
 * Copyright (c) 2008, Xiong Yingfei, the University of Tokyo
 * All rights reserved.
 */
package ac.jp.u_tokyo.SyncLib.dictionaries;

import java.util.Map;

import ac.jp.u_tokyo.SyncLib.CompilerParameters;
import ac.jp.u_tokyo.SyncLib.Mod;
import ac.jp.u_tokyo.SyncLib.SynchronizationFailedException;
import ac.jp.u_tokyo.SyncLib.util.TwoIterable;

public class InsertNewKeyDynamicGet<K> extends DynamicGet<K>
{
	KeyFactory<K> _keyFactory;

	public InsertNewKeyDynamicGet(boolean dictOverVal, KeyFactory<K> factory)
	{
		super(dictOverVal, true);
		_keyFactory = factory;
	}

	public InsertNewKeyDynamicGet(boolean dictOverVal, boolean changeKeyFirst, KeyFactory factory)
	{
		super(dictOverVal, changeKeyFirst);
		_keyFactory = factory;
	}

	@Override
	protected Mod[] insertNewKey(Mod[] modifications, DictMod<K, Object> dictMod, Map<K, Object> dict, Object value) throws SynchronizationFailedException
	{
		
		K newKey;
		if (CompilerParameters.UseGlobalKeyFactory)
			newKey = _keyFactory.createNewKey(null);
		else
			newKey = _keyFactory.createNewKey(new TwoIterable<K>(dict.keySet(), dictMod.getKeys()));
		return changeDictAndValueWithFixedKey(modifications, newKey, dict, value, false);
	}

	@Override
	public String toString()
	{
		return "InsertNewKeyDynamicGet@" + hashCode();
	}
	
	

}
